﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using KFC.MonAnF;

namespace KFC.MyUserControl
{
    public partial class ThemMonAnUSC : UserControl
    {
        private MonAn _monAn;
        private static Form1 form;

        private List<TextBox> textBoxs = new List<TextBox>();

        protected ThemMonAnUSC(Form1 f)
        {
            InitializeComponent();
            form = f;
     
        }

        private static ThemMonAnUSC _instance = null;

        public static ThemMonAnUSC Instance(Form1 f)
        {
            if (_instance == null)
                _instance = new ThemMonAnUSC(f);
            return _instance;
        }

        public void InitAll(MonAnF.MonAn monAn)
        {
            this.Controls.Clear();
            _monAn = monAn;
            List<string> result = monAn.GetAllAttributes();
            
            for (int i = 0; i < result.Count; i++)
            {
                Label label = new Label();
                label.AutoSize = true;
                label.Location = new System.Drawing.Point(15, 30 + 40 * i);
                label.Name = "label" + (61000 + i).ToString();
                label.Size = new System.Drawing.Size(35, 13);
                label.TabIndex = 62000 + i;
                label.Text = result[i];
                this.Controls.Add(label);
            }

            for (int i = 0; i < result.Count; i++)
            {
                TextBox textbox = new TextBox();
                textbox.Location = new System.Drawing.Point(90, 27 + 40 * i);
                textbox.Name = "textbox" + (63000 + i).ToString();
                textbox.Size = new System.Drawing.Size(100, 20);
                textbox.TabIndex = 64000 + i;
                this.Controls.Add(textbox);
                textBoxs.Add(textbox);
            }

            if (monAn.LayTenLoaiDoiTuong().Contains("PhanAn"))
            {
                Button buttonDS = new Button();
                buttonDS.Location = new System.Drawing.Point(30, 27 + 40 * result.Count);
                buttonDS.Name = "button" + (65000 + 1).ToString();
                buttonDS.Size = new System.Drawing.Size(150, 23);
                buttonDS.TabIndex = 66000 + 1;
                buttonDS.UseVisualStyleBackColor = true;
                buttonDS.Text = "Danh sách món ăn";
                this.Controls.Add(buttonDS);
                buttonDS.Click += buttonDS_Click;
                

                Button buttonAdd = new Button();
                buttonAdd.Location = new System.Drawing.Point(30, 27 + 40 * (result.Count + 1));
                buttonAdd.Name = "button" + (65000 + 2).ToString();
                buttonAdd.Size = new System.Drawing.Size(150, 23);
                buttonAdd.TabIndex = 66000 + 2;
                buttonAdd.UseVisualStyleBackColor = true;
                buttonAdd.Text = "Thêm";
                this.Controls.Add(buttonAdd);
                buttonAdd.Click += buttonAdd_Click;
            }
            else
            {
                Button buttonAdd2 = new Button();
                buttonAdd2.Location = new System.Drawing.Point(65, 27 + 40 * result.Count);
                buttonAdd2.Name = "button" + (65000 + 1).ToString();
                buttonAdd2.Size = new System.Drawing.Size(75, 23);
                buttonAdd2.TabIndex = 66000 + 1;
                buttonAdd2.UseVisualStyleBackColor = true;
                buttonAdd2.Text = "Thêm";
                this.Controls.Add(buttonAdd2);
                buttonAdd2.Click += buttonAdd2_Click;
            }
        }

        void buttonDS_Click(object sender, EventArgs e)
        {
            //((PhanAn)_monAn).DsMaMonAnDon;
            //((PhanAn)_monAn).DsSoLuongMon;

            QuanLyTaoPhanAn qltpa = new QuanLyTaoPhanAn();
            PhanAnBuilder phanAnBuilder;
            if (_monAn.LayTenLoaiDoiTuong() == "PhanAnCombo")
            {
                phanAnBuilder = new PhanAnComboBuilder();
                qltpa.TaoPhanAn(phanAnBuilder);
                _monAn = phanAnBuilder.GetResult();
            }
            if (_monAn.LayTenLoaiDoiTuong() == "PhanAnTreEm")
            {
                phanAnBuilder = new PhanAnTreEmBuilder();
                qltpa.TaoPhanAn(phanAnBuilder);
                _monAn = phanAnBuilder.GetResult();
            }
        }

        void buttonAdd_Click(object sender, EventArgs e)
        {
            ThemMon();
        }

        void buttonAdd2_Click(object sender, EventArgs e)
        {
            ThemMon();
        }

        public void ThemMon()
        {
            try
            {
                _monAn.MaMonAn = textBoxs[0].Text;
                _monAn.TenMonAn = textBoxs[1].Text;
                _monAn.DonGia = int.Parse(textBoxs[2].Text);
                _monAn.ChiTietMonAn = textBoxs[3].Text;
                _monAn.SoLuong = int.Parse(textBoxs[4].Text);
                _monAn.LoaiDoiTuong = _monAn.LayTenLoaiDoiTuong();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Điền sai thông tin, mời nhập lại!!");
                return;
            }
            QuanLyMonAn qlma = QuanLyMonAn.Instance();
            qlma.ThemMon(_monAn);

            MessageBox.Show("Bạn đã thêm món ăn thành công");
            form.RefershListViewDSMonAn();
        }

        private void ThemMonAnUSC_Load(object sender, EventArgs e)
        {

        }
    }
}
